using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;


namespace GarbageTruck.TemporalExpressions
{
    public class DayOfTheMonth : ITemporalExpression
    {

        #region CONSTRUCTORS

        public DayOfTheMonth()
        {
        }

        public DayOfTheMonth(string expresion)
        {
            string[] parameters = expresion.Split(',');

            this.Number = int.Parse(parameters[0]);
            if (parameters.Length > 1)
            {
                ignoreDay = false;

                this.Day = (DayOfWeek)int.Parse(parameters[1]);
            }
        }

        public DayOfTheMonth(int number)
        {
            this.Number = number;
        }

        public DayOfTheMonth(int number, DayOfWeek day)
        {
            this.Number = number;
            this.Day = day;
            ignoreDay = false;
        }

        #endregion CONSTRUCTORS

        #region FIELDS

        private int m_Number;
        private DayOfWeek m_Day;
        private bool ignoreDay = true;
        private Calendar calendar = CultureInfo.CurrentCulture.Calendar;

        #endregion FIELDS

        #region PROPERTIES

        public int Number
        {
            get { return m_Number; }
            set { m_Number = value; }
        }

        public DayOfWeek Day
        {
            get { return m_Day; }
            set { m_Day = value; }
        }

        #endregion PROPERTIES

        #region METHODS

        public bool Contains(DateTime dateTime)
        {
            if (ignoreDay)
            {
                if (Number > 0)
                {
                    return (Number == dateTime.Day);
                }
                else
                {
                    return ((calendar.GetDaysInMonth(dateTime.Year, dateTime.Month) + 1 + Number) == dateTime.Day);
                }
            }
            else
            {
                int value = (Number < 0 ? -1 : 1);
                int debug1 = calendar.AddWeeks(dateTime, -1 * Number).Month;
                int debug2 = calendar.AddWeeks(dateTime, -1 * Number + value).Month;

                return ((Day == dateTime.DayOfWeek) &&
                    (calendar.AddWeeks(dateTime, -1 * Number).Month != dateTime.Month) &&
                    (calendar.AddWeeks(dateTime, -1 * Number + value).Month == dateTime.Month));
            }
        }

        public DateTime? GetNextOcurrence(DateTime dateTime)
        {
            DateTime currentMonthOcurrence = new DateTime();
            DateTime nextMonthOcurrence = new DateTime();
            if (ignoreDay)
            {
                if (Number > 0)
                {
                    currentMonthOcurrence = new DateTime(dateTime.Year, dateTime.Month, Number);
                    nextMonthOcurrence = new DateTime(dateTime.AddMonths(1).Year, dateTime.AddMonths(1).Month, Number);
                }
                else
                {
                    currentMonthOcurrence = new DateTime(dateTime.Year, dateTime.Month,
                        (calendar.GetDaysInMonth(dateTime.Year, dateTime.Month) + 1 + Number));

                    nextMonthOcurrence = new DateTime(dateTime.AddMonths(1).Year, dateTime.AddMonths(1).Month,
                        (calendar.GetDaysInMonth(dateTime.AddMonths(1).Year, dateTime.AddMonths(1).Month) + 1 + Number));
                }
            }
            else
            {
                if (Number > 0)
                {
                    for (int i = 1; i < 10; i++)
                    {
                        currentMonthOcurrence = new DateTime(dateTime.Year, dateTime.Month, i);
                        if (currentMonthOcurrence.DayOfWeek == Day) break;
                    }

                    for (int i = 1; i < 10; i++)
                    {
                        nextMonthOcurrence = new DateTime(dateTime.AddMonths(1).Year, dateTime.AddMonths(1).Month, i);
                        if (nextMonthOcurrence.DayOfWeek == Day) break;
                    }

                    currentMonthOcurrence = calendar.AddWeeks(currentMonthOcurrence, Number - 1);
                    nextMonthOcurrence = calendar.AddWeeks(nextMonthOcurrence, Number - 1);
                }
                else
                {
                    for (int i = calendar.GetDaysInMonth(dateTime.Year, dateTime.Month); i > 20; i--)
                    {
                        currentMonthOcurrence = new DateTime(dateTime.Year, dateTime.Month, i);
                        if (currentMonthOcurrence.DayOfWeek == Day) break;
                    }

                    for (int i = calendar.GetDaysInMonth(dateTime.AddMonths(1).Year, dateTime.AddMonths(1).Month); i > 20; i--)
                    {
                        nextMonthOcurrence = new DateTime(dateTime.AddMonths(1).Year, dateTime.AddMonths(1).Month, i);
                        if (nextMonthOcurrence.DayOfWeek == Day) break;
                    }

                    currentMonthOcurrence = calendar.AddWeeks(currentMonthOcurrence, Number + 1);
                    nextMonthOcurrence = calendar.AddWeeks(nextMonthOcurrence, Number + 1);
                }
            }

            if (dateTime < currentMonthOcurrence)
                return currentMonthOcurrence;
            else
                return nextMonthOcurrence;
        }

        public DateTime? GetEventEnd(DateTime dateTime)
        {
            return dateTime;
        }

        public override string ToString()
        {
            return string.Format(ignoreDay ? "DOTM({0})" : "DOTM({0},{1})",
                                this.Number,
                                (int)this.Day);
        }

        #endregion METHODS

    }
}
